package org.eduapp.trains.service.train.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.eduapp.trains.Constants;
import org.eduapp.trains.DAOException;
import org.eduapp.trains.ServiceException;
import org.eduapp.trains.dao.train.TrainDAO;
import org.eduapp.trains.model.train.ModelTrain;
import org.eduapp.trains.service.train.TrainService;
import org.eduapp.trains.vo.VoTrainSearch;
import org.eduapp.trains.web.support.PaginationSupport;

public class TrainServiceImpl implements TrainService {
	
	private final static Logger logger = Logger.getLogger(TrainServiceImpl.class);
	
	private final String SEARCH_TYPE_BY_Number 			= "1";
	private final String SEARCH_TYPE_BY_Station_Single 	= "2";
	private final String SEARCH_TYPE_BY_Station_Pair 	= "3";
	
	private TrainDAO trainDAO;
	
	/*
	 * (non-Javadoc)
	 * @see org.eduapp.trains.service.train.TrainService#get(java.lang.String)
	 */
	@Override
	public ModelTrain get(String id) throws ServiceException {
		try {
			return trainDAO.get(id);
		} catch (DAOException e) {
			logger.error(e);
			e.printStackTrace();
		} catch (Exception e) {
			logger.error(e);
			throw (new ServiceException(e));
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eduapp.trains.service.train.TrainService#getByTrainNo(java.lang.String)
	 */
	@Override
	public ModelTrain getByTrainNo(String no) throws ServiceException {
		try {
			return trainDAO.getByTrainNo(no);
		} catch (DAOException e) {
			logger.error(e);
			e.printStackTrace();
		} catch (Exception e) {
			logger.error(e);
			throw (new ServiceException(e));
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eduapp.trains.service.train.TrainService#getAll()
	 */
	public List<ModelTrain> getAll () throws ServiceException {
		try {
			return trainDAO.getAll();
		} catch (DAOException e) {
			logger.error(e);
			e.printStackTrace();
		} catch (Exception e) {
			logger.error(e);
			throw (new ServiceException(e));
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eduapp.trains.service.train.TrainService#getTrainToBeUpdated()
	 */
	public List<ModelTrain> getTrainToBeUpdated() throws ServiceException {
		try {
			return trainDAO.getTrainToBeUpdated();
		} catch (DAOException e) {
			logger.error(e);
			e.printStackTrace();
		} catch (Exception e) {
			logger.error(e);
			throw (new ServiceException(e));
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eduapp.trains.service.train.TrainService#getBySearchVO(org.eduapp.trains.vo.VoTrainSearch)
	 */
	public PaginationSupport<ModelTrain> getBySearchVO (VoTrainSearch vo) throws ServiceException {
		
		if (vo != null) {
			
			vo.setTrainSearchType(vo.getTrainSearchType() != null ? vo.getTrainSearchType() : SEARCH_TYPE_BY_Number);
			
			if (vo.getTrainSearchType().equals(SEARCH_TYPE_BY_Number)) {
				// 车次查询
				ModelTrain model = getByTrainNo(vo.getTrainNumber());
				if (model != null) {
					List<ModelTrain> result = new ArrayList<ModelTrain>();
					result.add(model);
					return new PaginationSupport<ModelTrain> (result, 1, 0, 1);
				}
			} else if (vo.getTrainSearchType().equals(SEARCH_TYPE_BY_Station_Single)) {
				// 站名查询
				return getByStations(vo.getTrainStationName(), vo.getTrainOffset(), vo.getPageSize(), vo.getOrder(), vo.getIsDesc());
			} else if (vo.getTrainSearchType().equals(SEARCH_TYPE_BY_Station_Pair)) {
				// 站站查询
				return getByStation2Station(vo.getTrainStationFrom(), vo.getTrainStationTo(), vo.getTrainOffset(), vo.getPageSize(), vo.getOrder(), vo.getIsDesc(), vo);
			}
			
		}
		
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eduapp.trains.service.train.TrainService#remove(java.lang.String)
	 */
	@Override
	public void remove(String id) throws ServiceException {
		try {
			trainDAO.remove(get(id));
		} catch (DAOException e) {
			logger.error(e);
			e.printStackTrace();
		} catch (Exception e) {
			logger.error(e);
			throw (new ServiceException(e));
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eduapp.trains.service.train.TrainService#remove(org.eduapp.trains.model.train.ModelTrain)
	 */
	@Override
	public void remove(ModelTrain model) throws ServiceException {
		try {
			trainDAO.remove(model);
		} catch (DAOException e) {
			logger.error(e);
			e.printStackTrace();
		} catch (Exception e) {
			logger.error(e);
			throw (new ServiceException(e));
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eduapp.trains.service.train.TrainService#saveOrUpdate(org.eduapp.trains.model.train.ModelTrain)
	 */
	@Override
	public void saveOrUpdate(ModelTrain model) throws ServiceException {
		try {
			trainDAO.saveOrUpdate(model);
		} catch (DAOException e) {
			logger.error(e);
			e.printStackTrace();
		} catch (Exception e) {
			logger.error(e);
			throw (new ServiceException(e));
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eduapp.trains.service.train.TrainService#getByStation2Station(java.lang.String, java.lang.String)
	 */
	@Override
	public PaginationSupport<ModelTrain> getByStation2Station(String stationSrc,
			String stationDst) throws ServiceException {
		return getByStation2Station(stationSrc, stationDst, 0, Constants.DefaultPageSize, "train_number", false);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eduapp.trains.service.train.TrainService#getByStation2Station(java.lang.String, java.lang.String, int, int, java.lang.String, java.lang.Boolean)
	 */
	@Override
	public PaginationSupport<ModelTrain> getByStation2Station (String stationSrc, String stationDst,
			int offset, int pageSize, String order, Boolean isDesc) throws ServiceException {
		return getByStation2Station(stationSrc, stationDst, offset, pageSize, order, isDesc, null);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eduapp.trains.service.train.TrainService#getByStation2Station(java.lang.String, java.lang.String, int, int, java.lang.String, java.lang.Boolean, org.eduapp.trains.vo.VoTrainSearch)
	 */
	public PaginationSupport<ModelTrain> getByStation2Station (String stationSrc, String stationDst, 
			int offset, int pageSize, String order, Boolean isDesc, VoTrainSearch vo) throws ServiceException {
		try {
			return trainDAO.getByStation2Station(stationSrc, stationDst, offset, pageSize, order, isDesc, vo);
		} catch (DAOException e) {
			logger.error(e);
			e.printStackTrace();
		} catch (Exception e) {
			logger.error(e);
			throw (new ServiceException(e));
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eduapp.trains.service.train.TrainService#getByStations(java.lang.String)
	 */
	@Override
	public PaginationSupport<ModelTrain> getByStations(String stationName)
			throws ServiceException {
		try {
			return trainDAO.getByStations(stationName, 0, Constants.DefaultPageSize, null, null);
		} catch (DAOException e) {
			logger.error(e);
			e.printStackTrace();
		} catch (Exception e) {
			logger.error(e);
			throw (new ServiceException(e));
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eduapp.trains.service.train.TrainService#getByStations(java.lang.String, int, int, java.lang.String, java.lang.Boolean)
	 */
	public PaginationSupport<ModelTrain> getByStations (String stationName, int offset, int pageSize, String order, Boolean isDesc) 
		throws ServiceException {
		try {
			return trainDAO.getByStations(stationName, offset, pageSize, order, isDesc);
		} catch (DAOException e) {
			logger.error(e);
			e.printStackTrace();
		} catch (Exception e) {
			logger.error(e);
			throw (new ServiceException(e));
		}
		return null;
	}

	public void setTrainDAO(TrainDAO trainDAO) {
		this.trainDAO = trainDAO;
	}

	public TrainDAO getTrainDAO() {
		return trainDAO;
	}

	public static Logger getLogger() {
		return logger;
	}

}
